package com.example.bookreader1130_1203.ui.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.bookreader1130_1203.Adapter.BookAdapter;
import com.example.bookreader1130_1203.Adapter.CollectionAdapter;
import com.example.bookreader1130_1203.Bean.Book;
import com.example.bookreader1130_1203.Bean.ChooseBook;
import com.example.bookreader1130_1203.Bean.CollectionItem;
import com.example.bookreader1130_1203.R;
import com.example.bookreader1130_1203.ReadActivity;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    private ListView booklv ;
    List<Book> bookList=new ArrayList<>();
    List<ChooseBook> chooseBookList=new ArrayList<>();
    List<CollectionItem>collectionItemList=new ArrayList<>();
    CollectionAdapter adapter ;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ititData();
        initView(view);
        setListener();
        return view;
    }

    private void ititData() {
        SharedPreferences sp=getActivity().getSharedPreferences("userinfo",Context.MODE_PRIVATE);
        int uid =sp.getInt("userid",-1);
        /*所有书籍拿出来*/
        chooseBookList =LitePal.where("userid=?",uid+"").find(ChooseBook.class);
        bookList=LitePal.findAll(Book.class);

        for (Book b:bookList){
            Boolean k=false ;  //一个值为false
            for ( ChooseBook c:chooseBookList){
                if (b.getId()==c.getBookid()){
                    k=true ; /* id相符则为true   如果ID不相符合就为false*/
                }
            }

            CollectionItem item =new CollectionItem(b.getBookimg(),b.getBookname(),b.getAuthorname(),k);
            collectionItemList.add(item) ;

        }

    }
    private void setListener() {
        booklv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override                                           /* */
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                /*做一个跳转  跳转到看书的页面*/
                Intent intent =new Intent( getContext(), ReadActivity.class) ;
                /*传入书名 ，因为我们要哦是书名*/
                intent.putExtra("bookname" ,collectionItemList.get(position).getBookname());
                startActivity(intent);
            }
        });
        booklv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                SharedPreferences  sp=getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
                int uid =sp.getInt("userid",-1);
                int bid=bookList.get(position).getId();
                if (isChoose(uid,bid)){
                    /*如果没有重复的就，*/
                    new ChooseBook(0,uid,bid).save();
                    /*            */
                    collectionItemList.get(position).setIscollected(true);
                    adapter.notifyDataSetChanged();
                    showToast("收藏成功");
                    /* AIT+回车 在目标里创建一个方法*/

                }
                test();//测试方法
                return true;  //false -true
            }
        });
    }
    private void test() {//测试
        for ( ChooseBook c:LitePal.findAll(ChooseBook.class)){
            Log.e("choosebook",c.getId()+" "+c.getUserid()+" "+c.getBookid());
            /*检查 ID是否重复      */
        }
    }

    private void showToast(String str) {/*弹窗*/
        Toast.makeText(getContext(),str,Toast.LENGTH_SHORT).show();
    }

    private boolean isChoose(int uid, int bid) {
        for (ChooseBook c: LitePal.findAll(ChooseBook.class)){

            if (c.getUserid()==uid&&c.getBookid()==bid){
                return false;

            }
        }
        return true;
        /*方法里执行return就会打断后面的流程*/
    }

    private void initView(View view) {
        booklv=view.findViewById(R.id.booklv) ;
        bookList= LitePal.findAll(Book.class);
        adapter=new CollectionAdapter(getContext(),R.layout.collection_item,collectionItemList);
        booklv.setAdapter(adapter);
    }
}


